package cn.fl.dk.web.service.calculator.impl;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.dk.facade.constant.DkQuestneLogConstant;
import cn.fl.dk.web.service.calculator.ScoreCalculator;
import cn.fl.dk.web.vo.CalculateResult;
import fl.score.mgt.facade.score.score.api.ISysScoreIdxOptQueryApiService;
import fl.score.mgt.facade.score.score.api.ISysScoreIdxQueryApiService;
import fl.score.mgt.facade.score.score.dto.SysScoreIdxDTO;
import fl.score.mgt.facade.score.score.dto.SysScoreIdxOptDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 已出险问卷分数计算方式
 * 作者:hongyanbo
 * 时间:2018/6/8
 */
@Service("examAScoreCalculator")
public class ExamAScoreCalculator implements ScoreCalculator{

    @Autowired
    private ISysScoreIdxQueryApiService idxQueryApiService;
    @Autowired
    private ISysScoreIdxOptQueryApiService optQueryApiService;
    // 4==a
    private boolean flag1 =false;
    // 5==c
    private boolean flag2 =false;
    /**
     * 1	已保存
     * 2	预期逾期追加
     * 3	预期拖回
     * 4	预期还款
     * 5	出险严重
     * 6	出险其他
     * @param list
     * @return

     */
    @Override
    public CalculateResult cal(List<KeyValueVO> list) {
        flag1=false;
        flag2=false;
        CalculateResult result = new CalculateResult();
        for (KeyValueVO vo : list) {
            long idxId = Long.parseLong(vo.getKey());
            long optId = Long.parseLong(vo.getValue());
            SysScoreIdxDTO idxDTO = idxQueryApiService.selectSysScoreIdxDTOById(idxId);
            SysScoreIdxOptDTO optDTO = optQueryApiService.selectSysScoreIdxOptDTOById(optId);
            if(idxDTO!=null && optDTO!=null){
                Long idxNo = idxDTO.getSortNo();
                Long optNo = optDTO.getSortNo();
                // 出险严重的情况
                if(idxNo == 1 && optNo==2){ // 问题1=B
                    result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_5));
                    return result;
                }
                if(idxNo == 2 && optNo==1) { // 2==A
                    result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_5));
                    return result;
                }
                if(idxNo == 6 && optNo==1) { // 6==A
                    result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_5));
                    return result;
                }
                if(idxNo == 7 && optNo==1) { // 7==A
                    result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_5));
                    return result;
                }

                if(idxNo == 4 && optNo==1 ){ // 4==A
                    flag1=true;
                }
                if(idxNo == 5 && optNo==3 ){ // 5==c
                    flag2=true;
                }
            }
        }

        if(flag1){ //4==A && 5==c
            if(flag2){
                result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_5));
            }else{
                result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_6));
            }
        }else{ //其他情况
            result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_6));
        }
        return result;
    }
}
